package com.rayboo.example.collections.map;

import java.util.concurrent.ConcurrentHashMap;

/**
 * ConcurrentHashMap 示例
 *
 * @author ray
 * @since 2019/07/25
 *
 * <p>ConcurrentHashMap类是在JDK 1.5中引入的，它实现了ConcurrentMap以及Serializable接口。
 * ConcurrentHashMap是对HashMap的增强，因为我们知道在我们的应用程序中处理Threads时HashMap不是一个好的选择，因为性能明智的HashMap不是最好的。
 *
 * <p>https://www.geeksforgeeks.org/concurrenthashmap-in-java/
 *
 * <p>http://www.codeceo.com/article/java-hashmap-concurrenthashmap.html
 *
 * <p>https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/index.html
 */
public class ConcurrentHashMapExample {

    public static void main(String[] args) {
        ConcurrentHashMap map = new ConcurrentHashMap();
        map.put(100, "Hello");
        map.put(101, "Geeks");
        map.put(102, "Geeks");

        // Here we cant add Hello because 101 key
        // is already present in ConcurrentHashMap object
        map.putIfAbsent(101, "Hello");

        // We can remove entry because 101 key
        // is associated with For value
        map.remove(101, "Geeks");

        // Now we can add Hello
        map.putIfAbsent(103, "Hello");

        // We cant replace Hello with For
        map.replace(101, "Hello", "For");
        System.out.println(map);
    }

}
